//
//  UIView+Factory.m
//  CaiCaiYunNotes
//
//  Created by user on 13-12-23.
//  Copyright (c) 2013年 leo. All rights reserved.
//

#import "UIView+Extend.h"
#import "UIColor+HexString.h"
#define TAG_REMIND 3456

@implementation UIView (Extend)

- (void)wobble:(BOOL)wobble clockwise:(BOOL)clockwise
{
    if (wobble){
        self.transform = CGAffineTransformMakeRotation(clockwise?0.1:-0.1);
        
        [UIView animateWithDuration:0.1
                              delay:0
                            options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse|UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveLinear
                         animations:^{
                             self.transform = CGAffineTransformMakeRotation(clockwise?-0.1:0.1);
                         } completion:nil];
        
    }
    else{
        [UIView animateWithDuration:0.25
                              delay:0
                            options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             self.transform = CGAffineTransformIdentity;
                             
                         } completion:nil];
    }
}

-(void)showRemind:(BOOL)remind center:(CGPoint)center{
    UIView *view = [self viewWithTag:TAG_REMIND];
    if (!view && remind) {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 8)];
        view.layer.cornerRadius = 4;
        view.tag = TAG_REMIND;
        [view setBackgroundColor:[UIColor colorWithHexString:@"#f53d59"]];
        [self addSubview:view];
        view.center = center;
    }else if(view && !remind){
        [view removeFromSuperview];
    }else if(remind){
        view.center = center;
    }
}
//带标题的小红点
-(void)showRemind:(BOOL)remind title:(NSString *)title center:(CGPoint)center
{
    UILabel * label = [self viewWithTag:TAG_REMIND];
    if (!label && remind) {
        CGFloat viewW = 20;
        label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, viewW, viewW)];
        [label setText:title];
        [label setTextAlignment:NSTextAlignmentCenter];
        label.textColor = [UIColor whiteColor];
        label.font = FONT_DEFAULT_Light(10);
        label.layer.cornerRadius = viewW / 2;
        label.layer.masksToBounds = YES;
        label.tag = TAG_REMIND;
        label.layer.borderColor = [UIColor whiteColor].CGColor;
        label.layer.borderWidth = 2;
        [label setBackgroundColor:[UIColor colorWithHexString:@"#f53d59"]];
        [self addSubview:label];
        label.center = center;
    }else if(label && !remind)
    {
        [label removeFromSuperview];
    }else if(remind)
    {
        [label setText:title];
        label.center = center;
    }
}
-(void)addTapSingleAction:(void (^)(UITapGestureRecognizer *))action{
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] init];
    [recognizer onlyHangdleUIGestureRecognizerWithBlock:action];
    [self addGestureRecognizer:recognizer];
}

@end
